#include <iostream>

class Complex
{
public:
    Complex():real(0),vir(0){}
    Complex(int real,int vir):real(real),vir(vir){}

    Complex operator+(const Complex& c)//因为输入的变量不能轻易修改，加const
    {
        Complex result;//构建一个对象
        result.real = c.real + this->real;//当前的实部加上传进来的参数的实部
        result.vir = c.vir + this->vir;//当前的虚部加上传进来的参数的虚部
        return result;//不能对局部变量引用，函数结束就释放了

    }
    Complex operator-(const Complex& c)
    {
        Complex result;
        result.real = this->real - c.real;//当前的实部减去传进来的实部
        result.vir = this->vir - c.vir;//当前的虚部减传进来的虚部
        return result;
    }

    //后置++
    Complex operator++(int)//占位参数，与前置++进行区分
    {
        std::cout<<"后置++\n";
        Complex result = *this;//右值调带参，左值调拷贝，构造之后一定是运算符重载
        this->real++;
        this->vir++;
        return result;
    }
    
    //前置++
    Complex& operator++()//返回自己，不是局部变量，可以加引用
    {
        std::cout<<"前置++\n";
        this->real++;
        this->vir++;
        return *this;//左值返回会拷贝，右值返回会  加引用是为了防止拷贝
    }

    void show()
    {
        std::cout<<real<<"+"<<vir<<"i"<<std::endl;
    }

    friend std::ostream& operator<<(std::ostream& os,const Complex&c);
    friend std::istream& operator>>(std::istream& is, Complex&c);

private:
    int real;
    int vir;
};

//全局重载；调用类的对象，要加上友元   返回std::ostream是为了在调用时候可以连续输出
std::ostream& operator<<(std::ostream& os,const Complex&c)//为什么要多传一个参数
    {
        os<<c.real<<"+"<<c.vir<<"i";
        return os;
    }
std::istream& operator>>(std::istream& is, Complex&c)//不加const时因为要输入需要改他的值
{
    is>>c.real>>c.vir;
    return is;
}

int main()
{
    Complex c1(1,2);
    Complex c2(2,3);
    c1.show();
    c2.show();
    //Complex c= c1.add(c2);
    // Complex c= c1 + c2;//c1.operator+(c2)
    // Complex c= c1 - c2;//c1.operator-(c2)
    // c.show();

    // c2 = c1++;
    // c2.show();

    c2 = ++c1;
    // c2.show();
    std::cout<<c2<<std::endl;

    std::cin>>c2;
    return 0;
}